package fct.mpc.snipehunter;

import java.util.ArrayList;

import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.WindowManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import fct.mpc.utils.HuntingGrounds;
import fct.mpc.utils.HuntingMapView;
import fct.mpc.utils.SnipeMarker;

public class SnipeHunterHuntingActivity extends MapActivity
{
    //private SensorManager sensorMngr;
    private LocationManager locationMngr;
    private HuntingMapView view;
    private MapView mapView;
    private MyLocationOverlay myLocationOverlay;
    
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);

        //sensorMngr = (SensorManager) getSystemService(SENSOR_SERVICE);
        locationMngr = (LocationManager) getSystemService(LOCATION_SERVICE);
        
        view = new HuntingMapView(this);
        // Fred
        mapView = new MapView(this, "0sNGy2eXA0h5k0is59K2Q0ZmRm_D6-0vgXFOZaQ");
        // Nuno
        // mapView = new MapView(this, "0wvgz5JAwe6Av8VtMHjLmiZaVVRpPSpV8l-tNbA");       
        view.addView(mapView);
        
        setContentView(view);

        myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.runOnFirstFix(new Runnable()
        {
        	public void run()
        	{
        		mapView.getController().animateTo(myLocationOverlay.getMyLocation());
        	}
    	});
        
        SnipeMarker snipeOverlay = new SnipeMarker(getResources().getDrawable(R.drawable.snipe));
        ArrayList<GeoPoint> snipes = HuntingGrounds.getSnipeLocations();   
        
		for(int i=0; i<snipes.size(); i++)
		{		
			OverlayItem overlayItem = new OverlayItem(snipes.get(i), "", null); 
			snipeOverlay.addOverlay(overlayItem);
			mapView.getOverlays().add(snipeOverlay);
		}
        
        mapView.getOverlays().add(myLocationOverlay);
        mapView.getController().setZoom(18);
        mapView.setSatellite(true);
        mapView.setClickable(true);
        mapView.setEnabled(true);
        
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
	
    @Override
    protected void onResume()
    {
        super.onResume();
        
        Criteria c = new Criteria();
        c.setAccuracy(Criteria.ACCURACY_FINE);
        c.setPowerRequirement(Criteria.NO_REQUIREMENT);
        
        String provider = locationMngr.getBestProvider(c, true);
        
        //sensorMngr.registerListener(view, SensorManager.SENSOR_ORIENTATION, SensorManager.SENSOR_DELAY_UI);
        locationMngr.requestLocationUpdates(provider, 0, 0, view);
        
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableCompass();
    }

    @Override
    protected void onStop() 
    {
    	//sensorMngr.unregisterListener(view);
    	locationMngr.removeUpdates(view);
    	
        myLocationOverlay.disableMyLocation();
        myLocationOverlay.disableCompass();
        
        super.onStop();
    }
    
    @Override
    protected void onPause() 
    {
    	//sensorMngr.unregisterListener(view);
    	locationMngr.removeUpdates(view);
    	
    	myLocationOverlay.disableMyLocation();
    	myLocationOverlay.disableCompass();
    	
    	super.onPause();
    }
    
    @Override
    protected void onDestroy()
    {
    	getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    	
    	//sensorMngr.unregisterListener(view);
		locationMngr.removeUpdates(view);
    	
    	myLocationOverlay.disableMyLocation();
    	myLocationOverlay.disableCompass();
    	
    	super.onDestroy();
    }

    @Override
    protected boolean isRouteDisplayed() 
    {
        return false;
    }
}
